﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Mtbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Mtbl
{
    public class mtblProvinceDAL:ImtblProvince
    {
        HKEA_CMSEntities model;
        mtblProvince province;
        public mtblProvinceDAL()
        {
            model = DataAccess.GetModelInstance();
        }
        public static mtblProvinceDAL GetInstance()
        {
            return new mtblProvinceDAL();
        }
        public int InsertENT(mtblProvince ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.mtblProvinces.AddObject(ent);
            return model.SaveChanges();
        }

        public List<mtblProvince> GetListByENT(mtblProvince ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.mtblProvinces.Where(a => a.ProvinceUID !="");
            if (!string.IsNullOrEmpty(ent.ProvinceUID))
                result = result.Where(a => a.ProvinceUID == ent.ProvinceUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }
        public List<mtblProvince> GetListByENT(mtblProvince ent)
        {
            var result = model.mtblProvinces.Where(a => a.ProvinceUID != "");
            if (!string.IsNullOrEmpty(ent.ProvinceUID))
                result = result.Where(a => a.ProvinceUID == ent.ProvinceUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }
        public int GetCountByENT(mtblProvince ent)
        {
            var result = model.mtblProvinces.Where(a => a.ProvinceUID != "");
            if (!string.IsNullOrEmpty(ent.ProvinceUID))
                result = result.Where(a => a.ProvinceUID == ent.ProvinceUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(mtblProvince ent)
        {
            province = model.mtblProvinces.Where(a=>a.ProvinceUID==ent.ProvinceUID).FirstOrDefault();
            if (province != null)
            {
                province.Deleted = ent.Deleted;
                province.LastModifyBy = ent.LastModifyBy;
                province.LastModifyDate = DateTime.Now;
                province.Province_Chi = ent.Province_Chi;
                province.Province_Eng = ent.Province_Eng;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(mtblProvince ent)
        {
            province = model.mtblProvinces.Where(a => a.ProvinceUID == ent.ProvinceUID).FirstOrDefault();
            if (province != null)
            {
                model.mtblProvinces.DeleteObject(province);
            }
            return model.SaveChanges();
        }
    }
}
